package cn.edu.xjtu.nbrash.xbarsuite.floatbutton;

import java.io.File;

import cn.edu.xjtu.nbrash.xbarsuite.utilities.RootUtil;
import cn.edu.xjtu.nbrash.xbarsuite.utilities.ShellUtils;
import cn.edu.xjtu.nbrash.xbarsuite.utilities.ShellUtils.CommandResult;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

/**
 * ScreenShotUtil
 * 
 * @author totran
 *
 */

public class ScreenShotUtil {

	public static Uri takeScreenShot(String tempPath) {
		if (RootUtil.checkRootPermission()) {
			if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
				CommandResult commandResult = ShellUtils.execCommand(
						new String[] { "/system/bin/screencap -p " + tempPath,
								"chmod 666 " + tempPath }, true);
				if (commandResult.result == 0) {
					return Uri.fromFile(new File(tempPath));
				}
			}
		}

		return null;
	}

	/**
	 * Take a screenshot from your device
	 * 
	 * This method may cost long CPU time, so be careful
	 * 
	 * @param context
	 * @return
	 */
	public static Bitmap takeScreenShot(Context context) {
		if (RootUtil.checkRootPermission()) {
			if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
				return screenShot(context.getFilesDir().getPath() + "/temp"
						+ ".png");
			}
		}

		return screenShot(context);
	}

	/**
	 * This method is used in the rooted devices Android 4.0+
	 * 
	 * @param tempPath
	 * @return
	 */
	private static Bitmap screenShot(String tempPath) {
		CommandResult commandResult = ShellUtils.execCommand(
				new String[] { "/system/bin/screencap -p " + tempPath,
						"chmod 666 " + tempPath }, true);
		if (commandResult.result == 0) {
			return BitmapFactory.decodeFile(tempPath);
		}

		return null;
	}

	/**
	 * To be continue
	 * 
	 * @param context
	 * @return
	 */
	private static Bitmap screenShot(Context context) {

		return null;
	}
}
